﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Wiwi.Models;
using Wiwi.ViewModels;
using System.Web.Mvc;
using System.Web.Security;
namespace Wiwi.Controllers
{
    public static class CommonFunc
    {
        public static string GetListOfUsersForWant(Guid wntID, Guid UsrID, WiwiEntities1 db1)
        {
            string strRet = "";
            var UsrList = (from us in db1.A_Want_Wanter where us.WantID == wntID && us.UserID != UsrID select us.UserID).ToList();
            foreach (var usr in UsrList)
            {
                strRet += usr.ToString() + "?";
            }
            return strRet;
        }

        public static ProfileDetails FillProfileCounters(Guid UsrID, WiwiEntities1 db1)
        {
            ProfileDetails pv = new ProfileDetails();
            var ExpWiwi = (from ev in db1.A_Want_Wanter where ev.UserID == UsrID && ev.ExpiryDate < DateTime.Now select ev.Want_Wanter_RelID).Count();
            var CloseWiwi = (from ev in db1.A_Want_Wanter where ev.UserID == UsrID && ev.IsClosed ==true && ev.ExpiryDate > DateTime.Now select ev.Want_Wanter_RelID).Count();
            pv.ExpireWiwiCnt = ExpWiwi + CloseWiwi;
            var CurWants = (from ev in db1.A_Want_Wanter where ev.UserID == UsrID && ev.IsClosed != true && ev.ExpiryDate > DateTime.Now select ev.Want_Wanter_RelID).Count();
            pv.WantCnt = CurWants;
            var newPitches = (from wu in db1.A_vUserPitches where wu.UserID == UsrID && wu.PitchStatus == null select wu).Count();
            pv.pitchCnt = newPitches;
            var Notification = (from no in db1.A_Want_SellerCommunication where no.UserID == UsrID && no.IsRead == false select no.IsRead).Count();
            pv.NotificationCnt = Notification;
            var MatchingWiwiCnt = (from no in db1.A_MatchingWiwiCount(UsrID) select no).Count();
            pv.MatchingWiwiCnt = MatchingWiwiCnt;
            return pv;
        }

        public static List<A_vNewPitchesDetails> GetListOfPitchesDetailsForSupp(Guid wntID, Guid SuppID, WiwiEntities1 db1)
        {
            //add filter for decline pitch
            List<A_vNewPitchesDetails> det = new List<A_vNewPitchesDetails>();
            var UsrList = (from wu in db1.A_vNewPitchesDetails
                           where wu.WantID == wntID && wu.UserSellerID == SuppID select wu).ToList();
            det = UsrList;
            return det;
        }
        public static List<A_vNewPitchesDetails> GetListOfWantDetailsForUser(Guid wntID, Guid UsrID, WiwiEntities1 db1)
        {
            List<A_vNewPitchesDetails> det = new List<A_vNewPitchesDetails>();
            var UsrList = (from wu in db1.A_vNewPitchesDetails
                           where wu.WantID == wntID && wu.UserID == UsrID && wu.IsClosed==false && wu.PitchStatus ==true
                           select wu).ToList();
            det = UsrList;
            return det;
        }

        public static int GetNewPitchForWant(Guid wntUserID, WiwiEntities1 db1)
        {
            int strRet = 0;
            var UsrList = (from us in db1.A_Want_Wanter_Seller where us.Want_Wanter_RelID == wntUserID && us.PitchStatus == null select us.UserSellerID).ToList();
            if (UsrList.Any()) strRet = UsrList.Count;
            return strRet;
        }
        public static string GetListOfMetoForWant(Guid wntID, WiwiEntities1 db1, Guid UsrID)
        {
            string strRet = "";
            var UsrList = (from us in db1.A_Want_Wanter where us.WantID == wntID && us.UserID != UsrID && us.IsCreator == null select us.UserID).ToList();
            foreach (var usr in UsrList)
            {
                strRet += usr.ToString() + "?";
            }
            if (UsrList.Count > 0) strRet += "$" + UsrList.Count.ToString();
            return strRet;
        }
        public static int GetMetoCountForWant(Guid wntID, WiwiEntities1 db1)
        {
            int strRet = 0;
            var UsrList = (from us in db1.A_Want_Wanter where us.WantID == wntID && us.ExpiryDate > DateTime.Now && us.IsClosed ==false && us.IsCreator==null select us.UserID).ToList();
            if (UsrList.Count > 0) strRet = UsrList.Count;
            return strRet;
        }
        public static string GetWantpitchedForSupp(Guid wntRelID, Guid UsrID, WiwiEntities1 db1)
        {
            string strRet = "";
            A_Want_Wanter_Seller sel = db1.A_Want_Wanter_Seller.SingleOrDefault(c => c.Want_Wanter_RelID == wntRelID && c.UserSellerID == UsrID);
            if (sel != null)
            {
                if (sel.PitchStatus==true) strRet = "Accepted";
                else if (sel.PitchStatus == false) strRet = "Declined";
                else if (sel.PitchStatus == null) strRet = "Awaiting";
            }
            return strRet;
        }
    }

    public abstract class ApplicationController : Controller
    {
        WiwiEntities1 db = new WiwiEntities1();
        public ApplicationController()
        {
            ProfileDetails pd = new ProfileDetails();
            MembershipUser usr = Membership.GetUser();
            if (usr != null)
            {
                Guid gusr = new Guid(usr.ProviderUserKey.ToString());
                A_vUserTable UT = db.A_vUserTable.SingleOrDefault(c => c.UserID == gusr);
                pd.Usr = UT;
                ProfileDetails pfc = CommonFunc.FillProfileCounters(gusr, db);
                pd.pitchCnt = pfc.pitchCnt;
                pd.WantCnt = pfc.WantCnt;
                pd.ExpireWiwiCnt = pfc.ExpireWiwiCnt;
                pd.NotificationCnt = pfc.NotificationCnt;
                pd.MatchingWiwiCnt = pfc.MatchingWiwiCnt;
            }
            ViewData["ProfileDetails"] = pd;
        }

    }

    public class BinaryResult : ActionResult
    {
        private byte[] _fileBinary;
        private string _fileName;
        private string _Extn;
        public BinaryResult(byte[] fileBinary, string fileName,string extn)
        {
            _fileBinary = fileBinary;
            _fileName = fileName;
            _Extn = extn;
        }

        public override void ExecuteResult(ControllerContext context)
        {
            context.HttpContext.Response.Clear();
            switch (_Extn)
            {
                case ".doc":
                    context.HttpContext.Response.ContentType = "application/ms-word";
                    break;
                case ".docx":
                    context.HttpContext.Response.ContentType = "application/ms-word";
                    break;
                case ".xls":
                    context.HttpContext.Response.ContentType = "application/ms-excel";
                    break;
                case ".xlsx":
                    context.HttpContext.Response.ContentType = "application/ms-excel";
                    break;
                case ".pdf":
                    context.HttpContext.Response.ContentType = "Application/pdf";
                    break;
                case ".jpeg":
                    context.HttpContext.Response.ContentType = "image/jpeg";
                    break;
                case ".jpg":
                    context.HttpContext.Response.ContentType = "image/jpg";
                    break;
                case ".png":
                    context.HttpContext.Response.ContentType = "image/png";
                    break;
                case ".gif":
                    context.HttpContext.Response.ContentType = "image/gif";
                    break;
                case ".zip":
                    context.HttpContext.Response.ContentType = "application/zip";
                    break;
                case ".rar":
                    context.HttpContext.Response.ContentType = "application/x-rar-compressed";
                    break;
                case ".txt":
                    context.HttpContext.Response.ContentType = "application/octet-stream";
                    break;
            }
            context.HttpContext.Response.AddHeader("Content-Disposition", "filename=" + _fileName);
            if (_fileBinary != null)
            {
                context.HttpContext.Response.BinaryWrite(_fileBinary);
            }
        }
        
    }
}